//
//  FQDLabVoteResultViewController.m
//  FakeQDaily
//
//  Created by mewxiv on 9/18/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDLabVoteResultViewController.h"
#import "FQDCommentViewController.h"
#import "FQDLabVoteResultHeadCell.h"
#import "FQDSectionTitleCell.h"
#import "FQDLabVoteResultCell.h"
#import "FQDDetailToolBarButton.h"
#import "FQDDetailToolBar.h"
#import "FQDRestModel.h"

#import "Masonry.h"

static CGSize const kArticleToolBarSize = (CGSize){136.0f, 69.0f};

@interface FQDLabVoteResultViewController () <UICollectionViewDelegate,
                                              UICollectionViewDataSource,
                                              UICollectionViewDelegateFlowLayout>

// 返回按钮
@property (nonatomic, strong) UIButton *backButton;
// 右下角工具栏
@property (nonatomic, strong) FQDDetailToolBar *detailToolBar;
// 评论按钮
@property (nonatomic, strong) FQDDetailToolBarButton *commentButton;
// 顶部标题单元格，弹幕显示在上面
@property (nonatomic, weak) FQDLabVoteResultHeadCell *headCell;
@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation FQDLabVoteResultViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupCollectionView];
    [self setupDetailToolbar];
    [self updateDetailToolbar];
}

- (void)setupCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = 10.0f;
    flowLayout.minimumInteritemSpacing = 0;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, 80.0f, 0);
    self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[FQDLabVoteResultHeadCell class]
            forCellWithReuseIdentifier:[FQDLabVoteResultHeadCell reuseId]];
    [self.collectionView registerClass:[FQDLabVoteResultCell class]
            forCellWithReuseIdentifier:[FQDLabVoteResultCell reuseId]];
    [self.collectionView registerClass:[FQDSectionTitleCell class]
            forCellWithReuseIdentifier:[FQDSectionTitleCell reuseId]];
}


- (void)setupDetailToolbar
{
    UIImage *backImage = [UIImage imageNamed:@"navigation_back_round_normal"];
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.backButton setImage:backImage forState:UIControlStateNormal];
    [self.view addSubview:self.backButton];
    
    [self.backButton addTarget:self
                        action:@selector(goBack:)
              forControlEvents:UIControlEventTouchUpInside];
    
    WeakSelf;
    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.view).offset(14);
        make.bottom.equalTo(weakSelf.view).offset(-4);
    }];
    
    UIImage *commentImage = [UIImage imageNamed:@"toolbarComment"];
    FQDDetailToolBarButton *commentButton =
    [FQDDetailToolBarButton buttonWithIcon:commentImage
                                detailText:@""
                                    target:self
                                    action:@selector(showCommentView:)];
    self.commentButton = commentButton;
    
    UIImage *shareImage = [UIImage imageNamed:@"toolbarShare"];
    FQDDetailToolBarButton *shareButton =
    [FQDDetailToolBarButton buttonWithIcon:shareImage
                                detailText:@""
                                    target:self
                                    action:@selector(shareThisArticle:)];
    
    NSArray *barButtons = @[commentButton, shareButton];
    
    self.detailToolBar = [[FQDDetailToolBar alloc] initWithBarButtons:barButtons];
    [self.view addSubview:self.detailToolBar];
    
    [self.detailToolBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.view);
        make.bottom.equalTo(weakSelf.view);
        make.size.mas_equalTo(kArticleToolBarSize);
    }];
}


- (void)updateDetailToolbar
{
    NSInteger commentCount = self.commentCount;
    NSString *commentNumStr = (commentCount == 0) ? @"" : [@(commentCount) stringValue];
    self.commentButton.detailTextLabel.text = commentNumStr;
}


#pragma mark - Event response
- (void)goBack:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)showCommentView:(id)sender
{
    FQDCommentViewController *commentVC = [[FQDCommentViewController alloc] init];
    commentVC.post = self.post;
    [self.navigationController pushViewController:commentVC animated:YES];
}

- (void)shareThisArticle:(id)sender
{
    
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 2) {
        return [self.attitudes count];
    } else {
        return 1;
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell;
    if (indexPath.section == 0) {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:[FQDLabVoteResultHeadCell reuseId]
                                                         forIndexPath:indexPath];
        FQDLabVoteResultHeadCell *headCell = (FQDLabVoteResultHeadCell *)cell;
        headCell.option = self.option;
        self.headCell = headCell;
        
    } else if (indexPath.section == 1) {
        FQDSectionTitleCell *sectionTitleCell = (FQDSectionTitleCell *)
        [collectionView dequeueReusableCellWithReuseIdentifier:[FQDSectionTitleCell reuseId]
                                                  forIndexPath:indexPath];
        
        sectionTitleCell.titleLabel.text = @"大家的态度";
        cell = sectionTitleCell;
        
    } else if (indexPath.section == 2) {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:[FQDLabVoteResultCell reuseId]
                                                         forIndexPath:indexPath];
        FQDLabVoteResultCell *resultCell = (FQDLabVoteResultCell *)cell;
        FQDLabVoteAttitude *attitude = self.attitudes[indexPath.item];
        
        BOOL isMyChoice = NO;
        for (NSString *optionId in self.selectedOptionIds) {
            if ([optionId isEqualToString:attitude.optionID]) {
                isMyChoice = YES;
                break;
            }
        }
        resultCell.isMyChoice = isMyChoice;
        resultCell.attitude = attitude;
    }
    
    return cell;
}


#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.headCell) {
        CGFloat maxHeight = [FQDLabVoteResultHeadCell cellHeight];
        CGFloat offset = scrollView.contentOffset.y;
        if (maxHeight - offset >= 0) {
            CGRect frame = self.headCell.frame;
            frame.origin.y = offset;
            frame.size.height = maxHeight - offset;
            self.headCell.frame = frame;
        }
    }
}

#pragma mark - UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize size;
    CGFloat viewWidth = collectionView.frame.size.width;
    if (indexPath.section == 0) {
        size = CGSizeMake(viewWidth, [FQDLabVoteResultHeadCell cellHeight]);
    } else if (indexPath.section == 1) {
        size = CGSizeMake(viewWidth, 50.0f);
    } else if (indexPath.section == 2) {
        size = CGSizeMake(viewWidth, 85.0f);
    }
    return size;
}

@end